package com.pep.mgmt.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ShellCommandExecutor {
	
    public String executeCommand(String command) {
        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("bash", "-c", command);

        StringBuilder output = new StringBuilder();
        try {
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
            
            int exitVal = process.waitFor();
            if (exitVal != 0) {
                 // handle non-zero exit code if needed
             }

            String line;
            while ((reader.ready()) && ((line = reader.readLine()) != null)) {
                output.append(line).append("\n");
            }
 
        } catch (IOException | InterruptedException e) {
            Thread.currentThread().interrupt();
        }
 
        return output.toString();
    }
}